package com.stormrage.edu.dao.edu.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.edu.EduModuleDao;
import com.stormrage.entity.edu.EduModule;
import com.stormrage.entity.edu.rep.EduModuleRep;
import com.stormrage.entity.exam.rep.ExmExamCountRep;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Repository
public class EduModuleDaoImpl extends MysBaseDao implements EduModuleDao {

	private static final Logger logger = LoggerFactory.getLogger(EduModuleDaoImpl.class);

	@Override
	public Integer create(final EduModule arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 Create 】- 开始运行 ");

		final String query = "INSERT INTO edu_module (module, detail, ness) VALUES (:module, :detail, :ness)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduModule arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 Update 】- 开始运行 ");

		final String query = "UPDATE edu_module SET module = :module, detail = :detail, ness = :ness WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void delete(Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 Delete 】- 开始运行 ");

		final String query = "DELETE FROM edu_module WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, map);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryCount 】- 开始运行 ");

		final String query = "SELECT COUNT(*) FROM edu_module " + map.get("query");
		logger.debug(" 数据库执行语句为 " + query);

		Integer count = queryCount(query, map);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryCount 】- 完成运行 ");
		return count;
	}

	@Override
	public EduModule queryById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM edu_module WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EduModule entity = queryObject(query, EduModule.class, map);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public List<EduModule> queryList() throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM edu_module";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduModule> array = queryList(query, EduModule.class);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduModule> queryList(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryList 】- 开始运行 ");

		final String query = "SELECT * FROM edu_module" + map.get("query") + " LIMIT :startPage, :size";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduModule> array = queryList(query, EduModule.class, map);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduModule> queryListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryListById 】- 开始运行 ");

		final String query = "SELECT a.* FROM edu_module AS a, edu_major_module AS b"
				+ " WHERE a.id = b.module_id AND b.major_id = :id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduModule> array = queryList(query, EduModule.class, map);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryListById 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduModule> queryRepListByTerm(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 queryRepListByTermNess 】- 开始运行 ");

		final String query = "SELECT a.id, a.module FROM edu_module AS a, edu_major_module AS b, lea_term AS d"
				+ " WHERE a.id = b.module_id AND d.major_id = b.major_id AND d.id = :id AND a.ness = :ness"
				+ " ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduModule> array = queryList(query, EduModule.class, map);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 queryRepListByTermNess 】- 完成运行 ");
		return array;
	}

	@Override
	public List<ExmExamCountRep> queryRepListByTermNess(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 queryRepListByTermNess 】- 开始运行 ");

		final String query = "SELECT a.id, a.module AS name, c.sele, c.mult, c.corr FROM edu_module AS a,"
				+ " edu_major_module AS b, exm_relate_count AS c, lea_term AS d WHERE a.id = b.module_id"
				+ " AND c.relate_id = a.id AND d.major_id = b.major_id AND c.type = 1 AND d.id = :id"
				+ " AND a.ness = :ness ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<ExmExamCountRep> array = queryList(query, ExmExamCountRep.class, map);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 queryRepListByTermNess 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduModuleRep> queryRepListById(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryRepListById 】- 开始运行 ");

		final String query = "SELECT a.id, a.module, a.ness, b.id AS viewId FROM edu_module AS a, edu_major_module AS b"
				+ " WHERE a.id = b.module_id AND b.major_id = :id ORDER BY a.id";
		logger.debug(" 数据库执行语句为 " + query);

		List<EduModuleRep> array = queryList(query, EduModuleRep.class, map);

		logger.debug(" 【 Dao 】- 【 EduModuleDao 】- 【 QueryRepListById 】- 完成运行 ");
		return array;
	}

}
